package com.finace.money.presenter;

import android.os.SystemClock;

import com.finace.money.common.Constant;
import com.finace.money.common.util.ACache;
import com.finace.money.common.util.ChooseUtil;
import com.finace.money.common.util.DateUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.StatusListBean;
import com.finace.money.net.socket.PushReceiver;
import com.finace.money.ui.widget.BaseView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TreeSet;

/**
 * Created by zhangpeng on 2018/1/2.
 */

public class MainStatusPresenter extends BasePresenter {


    public MainStatusPresenter() {
        super();
    }

    public ArrayList<StatusListBean> getList() {

        ArrayList<StatusListBean> list = (ArrayList<StatusListBean>) ACache.get(UIUtil.getContext()).getAsObject(PushReceiver.ACTION_LIST);

        return list;
    }

    public ArrayList<String> sortTab(ArrayList<StatusListBean> statusLists) {
        ArrayList<String> datas = new ArrayList<>();
        ArrayList<String> names = new ArrayList<>();
        for (StatusListBean bean : statusLists) {
            datas.add(bean.getM_Body().getM_ExchangeName());
        }

        for (int i = 0; i < datas.size(); i++) {
            if(!names.contains(datas.get(i))){
                names.add(datas.get(i));
            }
        }
        return names;
    }

    public ArrayList<StatusListBean> getTabData(List<String> names, List<StatusListBean> statusLists, int position) {

        String mExchangeName = names.get(position);

        ArrayList<StatusListBean> statusListBeans = new ArrayList<>();
        for (StatusListBean bean : statusLists) {
            if (mExchangeName.equals(bean.getM_Body().getM_ExchangeName())) {
                statusListBeans.add(bean);
            }
        }
        if (ChooseUtil.getChooseCount() > 0) {
            List<StatusListBean> chooseLists = (List<StatusListBean>) ACache.get(UIUtil.getContext()).getAsObject(Constant.CHOOSE_ALL);
            for (StatusListBean chooseBean : chooseLists) {
                for (StatusListBean newBean : statusListBeans) {
                    if (newBean.getM_Body().getM_ProductID().equals(chooseBean.getM_Body().getM_ProductID())) {
                        newBean.setCheck(chooseBean.getCheck());
                    }
                }
            }
        }
        return statusListBeans;
    }

    public void setChoose(List<StatusListBean> statusLists, StatusListBean statusListBean) {

        ACache aCache = ACache.get(UIUtil.getContext());
        statusListBean.setCheck(!statusListBean.getCheck());
        aCache.put(PushReceiver.ACTION_LIST, (Serializable) statusLists);
        List<StatusListBean> chooseLists = new ArrayList<>();
        for (StatusListBean bean : statusLists) {
            if (bean.getCheck()) {
                chooseLists.add(bean);
            }
        }
        aCache.put(Constant.CHOOSE_ALL, (Serializable) chooseLists);
    }

}
